# Import standard packages
import sys
import logging
import argparse
import gettext
import locale
import os

# Import our own stuff
import backend.retrieveplan  

def init_localization():
    # use user's preferred locale
    logger.debug("Setting locale to " + str(locale.LC_ALL))
    locale.setlocale(locale.LC_ALL, '')
    
    # put together a filename appropriate to the users locale
    filename = os.path.join(os.getcwd(), "translations", "messages_%s.mo" % \
                            locale.getlocale()[0][0:2])

    # try to read from the file, if not readable, use standard locale
    logger.debug("Trying to read " + filename)
    try:
        trans = gettext.GNUTranslations(open( filename, "rb" ) )
    except IOError as e:
        logger.warn("Couldn't read translation file. Maybe there's no "+ \
                    "translation?")
        logger.debug("Here's what python says:")
        logger.debug(str(e))
        trans = gettext.NullTranslations()
    logger.info("Done reading translation file")        

    # Install global localization function
    trans.install()
    logger.info("Installed translation. Messages should appear in your " + \
                "systems language from now on")

def init_argparser():
    parser = argparse.ArgumentParser(description=\
                                     _("Retrieves Fahrwerks shift tables" + \
                                       "based on the actual date."))
    
    groups = parser.add_mutually_exclusive_group()
    
    guigroup = groups.add_argument_group(_("GUI"))
    guigroup.add_argument("-g", "--gui", action="store_true", help=_("Activate GUI-mode"))
    
    cmdgroup = groups.add_argument_group(_("Command line"))
    cmdgroup.add_argument("username", help=_("Username used to login"))
    cmdgroup.add_argument("password", help=_("Password used to login"))
    cmdgroup.add_argument("-d", "--debug", help=_("Activate debug mode"))
    
    parser.add_argument("-q", "--quiet", help=_("Suppress any console output." +\
                                                "Useful when invoked by cron."))

    parser.parse_args()

def main(argv):
    # Parse cmd-line arguments
    logger.debug("Initializing argument parsing")
    init_argparser()
    logger.debug("Done parsing arguments")
    
    # backend.retrieveplan.retrieveplan()

if __name__ == "__main__":
    # Initialize logging
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    
    fileHandler = logging.FileHandler("shiftcheck.log", "w")
    fileHandler.setLevel(logging.DEBUG)
    
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - "+ \
                                  "%(message)s")
    
    fileHandler.setFormatter(formatter)
    logger.addHandler(fileHandler)
    
    logger.info("Starting up")
    
    # Initialize localization
    logger.info("Initializing localization")    
    init_localization()
    
    # Start main program
    main(sys.argv)
